<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="加分数" prop="addPoit">
            <el-input v-model="form.addPoit" />
          </el-form-item>
          <el-form-item label="单位" prop="deptName">
            <div style="display:flex;">
              <el-input v-model="form.deptName" disabled />
              <el-button type="parimary" @click="deptVisible=true">选择</el-button>
            </div>
          </el-form-item>
          <el-form-item label="加分日期" prop="createTime">
            <el-date-picker v-model="form.createTime" placeholder="请选择" style="width: 100%;"></el-date-picker>  
          </el-form-item>
          <el-form-item label="加分说明">
            <el-input  :rows="3" type="textarea" v-model="form.explanation"  />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="addPoit" label="加分数" />
        <el-table-column label="单位" >
          <template scope="scope">{{ scope.row.dept&& scope.row.dept.name }} </template>
        </el-table-column>
        <el-table-column prop="explanation" label="加分说明" />
        <el-table-column v-permission="['admin','tUnitBonusPoints:edit','tUnitBonusPoints:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <selecrGropDept
      v-if="deptVisible"
      :dialog-visible.sync="deptVisible"
      title="选择单位"
      :multiple="false"
      :tag="tag"
      @select="selectDept"
    />
  </div>
</template>

<script>
import crudTUnitBonusPoints from '@/api/system/tUnitBonusPoints'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import selecrGropDept from './../components/selecrGropDept'
const defaultForm = {  addPoit: null, deptName: null, explanation: null,createTime:null }
export default {
  name: 'TUnitBonusPoints',
  components: { pagination, crudOperation, rrOperation, udOperation,selecrGropDept },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '单位加分管理', url: 'api/tUnitBonusPoints', idField: 'id', sort: 'id,desc', crudMethod: { ...crudTUnitBonusPoints }})
  },
  data() {
    const validatorNumber = (rule, value, callback) => {
      // 包含小数的数字
      const reg = /^[+]?(0|([1-9]\d*))(\.\d{0,2})?$/g
      if (value === '') {
        callback(new Error('请输入'))
      } else if (!parseInt(value * 100)) {
        callback(new Error('请输入有效数字或保留两位有效数字'))
      } else if (!reg.test(value)) {
        callback(new Error('请输入正整数或保留两位有效数字'))
      } else {
        callback()
      }
    }
    return {
      permission: {
        add: ['admin', 'tUnitBonusPoints:add'],
        edit: ['admin', 'tUnitBonusPoints:edit'],
        del: ['admin', 'tUnitBonusPoints:del']
      },
      rules: {
        createTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        addPoit: [
          { required: true, trigger: 'blur',validator:validatorNumber }
        ],
        deptName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ], 
      },
      tag:[],
      deptVisible:false
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    
    // 选择
    selectDept(row) {
      this.deptVisible = false 
      this.form.deptName = row.name
      this.form.dept = row
    },
    // 删除已选单位
    handleClose(tag) {
      if (this.eventVisible) {
        this.form.assistanceUnit.splice(this.form.assistanceUnit.indexOf(tag), 1)
        this.tag = this.form.assistanceUnit
      } else if (this.assignVisible) {
        this.assignform.assistanceUnit.splice(this.assignform.assistanceUnit.indexOf(tag), 1)
        this.tag = this.assignform.assistanceUnit
      }
    },
    // 打开编辑弹窗前做的操作
    [CRUD.HOOK.beforeToEdit](crud, form) { 
      this.form.deptName = form.dept && form.dept.name 
    },
  }
}
</script>
<style scoped>
</style>
